﻿package mortal.game.view.autoArena
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.autoArena.util.*;
    import mortal.game.view.autoArena.view.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.task.drama.operations.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AutoArenaController extends CopyControllerBase
    {
        private var _autoArenaModule:AutoArenaModule;
        public static var currChallengeEntityId:SEntityId;

        public function AutoArenaController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._autoArenaModule == null)
            {
                this._autoArenaModule = new AutoArenaModule();
                this._autoArenaModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._autoArenaModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._autoArenaModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ShopMallOpenAndSelect, this.openArenaWin);
            Dispatcher.addEventListener(EventName.AutoArenaEnterCopy, this.onEnterAutoArenaCopy);
            NetDispatcher.addCmdListener(ServerCommand.AutoArenaShowResult, this.onShowResultInfo);
            NetDispatcher.addCmdListener(ServerCommand.AutoArenaUpdateDayReward, this.onDayRewardUpdate);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.onInitIcon);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.onDateChange);
            Dispatcher.addEventListener(EventName.HourChange, this.onHourTimeChange);
            NetDispatcher.addCmdListener(ServerCommand.AutoArenaResetPowerFail, this.onResetPowerFail);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            if (!GameController.autoArena.view.isHide)
            {
                GameController.autoArena.view.hide();
            }
            if (AutoArenaResultWinNew.isViewShow)
            {
                AutoArenaResultWinNew.instance.hide();
            }
            AutoArenaVsInfoSpr.instance.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupAvatarVisible, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.SkillClearAllCD));
            TaskDramaMouseKeyboardControl.enablePlayerOp = false;
            cache.skill.needDisableUseSkill = true;
            AIManager.cancelAll();
            RolePlayer.instance.stopMove();
            GameMapUtil.isCanMoveOnClickMap = false;
            this.isNeedHideUI(true);
            if (MountUtil.isRideUp)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MountRide));
            }
            var _loc_2:* = LayerManager.uiLayer;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.numChildren)
            {
                
                _loc_2.getChildAt(_loc_3).visible = false;
                _loc_3++;
            }
            return;
        }// end function

        private function isNeedHideUI(param1:Boolean) : void
        {
            if (param1)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
                cache.best.copyBestViewClose = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.BestViewOpenClose));
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, false));
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
                cache.best.copyBestViewClose = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.BestViewOpenClose));
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, true));
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            AutoArenaVsInfoSpr.instance.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupAvatarVisible, true));
            TaskDramaMouseKeyboardControl.enablePlayerOp = true;
            cache.skill.needDisableUseSkill = false;
            GameMapUtil.isCanMoveOnClickMap = true;
            this.isNeedHideUI(false);
            var _loc_1:* = LayerManager.uiLayer;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.numChildren)
            {
                
                _loc_1.getChildAt(_loc_2).visible = true;
                _loc_2++;
            }
            return;
        }// end function

        private function onEnterAutoArenaCopy(event:DataEvent) : void
        {
            CopyUtil.copyCheck(this.enterAutoArenaCopy);
            return;
        }// end function

        private function enterAutoArenaCopy() : void
        {
            GameProxy.copy.enterCopy(GameConst.AutoArenaCopyCode);
            return;
        }// end function

        private function onShowResultInfo(param1:Object) : void
        {
            AutoArenaResultWinNew.instance.show();
            var _loc_2:* = cache.autoArena.sAutoArenaResult;
            if (_loc_2 && _loc_2.gold > 0)
            {
                AutoArenaNewRecordReward.instance.show();
            }
            return;
        }// end function

        private function onDayRewardUpdate(param1:Object = null) : void
        {
            var _loc_2:* = Cache.instance.autoArena.sAutoArenaDayReward;
            if (_loc_2 && !_loc_2.hadGet)
            {
                if (!AutoArenaIcon.instance.isHide)
                {
                    AutoArenaIcon.instance.playEffect();
                }
            }
            else if (!AutoArenaIcon.instance.isHide)
            {
                AutoArenaIcon.instance.stopEffect();
            }
            return;
        }// end function

        private function onInitIcon(event:DataEvent = null) : void
        {
            var _loc_2:* = cache.role.entityInfo.level;
            var _loc_3:* = ClockManager.instance.serverOpenDateNum;
            if (!Global.isDebugModle)
            {
                if (_loc_2 >= GameConst.AutoArenaOpenLevel && _loc_3 >= 3 && _loc_3 <= 7)
                {
                    AutoArenaIcon.instance.show();
                    AutoArenaUtil.updateIconLeftTime();
                }
                else if (!AutoArenaIcon.instance.isHide)
                {
                    AutoArenaIcon.instance.hide();
                }
            }
            else if (_loc_2 >= GameConst.AutoArenaOpenLevel)
            {
                AutoArenaIcon.instance.show();
            }
            this.onDayRewardUpdate();
            return;
        }// end function

        private function onLevelUpdateHandler(param1:Object) : void
        {
            this.onInitIcon();
            return;
        }// end function

        private function onDateChange(param1:Object) : void
        {
            this.onInitIcon();
            return;
        }// end function

        private function onHourTimeChange(event:DataEvent) : void
        {
            if (!AutoArenaIcon.instance.isHide)
            {
                AutoArenaUtil.updateIconLeftTime();
            }
            return;
        }// end function

        private function onResetPowerFail(param1:Object) : void
        {
            GameProxy.autoArena.getMyInfo();
            return;
        }// end function

        private function openArenaWin(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            if (_loc_2 == "show me the autoArena")
            {
                view.show();
            }
            return;
        }// end function

    }
}
